package com.penghaisoft.microwms.base.web.data.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.penghaisoft.microwms.base.web.data.service.IMdModelService;
import com.penghaisoft.microwms.base.web.data.service.IMdProModelService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdModel;
import com.penghaisoft.microwms.common.data.model.MdModelMes;
import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;

/**
 * @Description 产品型号
 * @author luoteng
 * @date 2018年5月29日 下午2:33:39
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/data/model")
@Slf4j
public class MdModelController extends BaseController {
	@Autowired
	private IMdModelService mdModelService;

	@Autowired
	private IMesInterfaceService mesInterfaceService;

	@Resource
	private MesProperties mesProperties;
	
	@Autowired
	private IMdProModelService mdProModelService;

	/**
	 * 新增记录
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(MdModel mdModel) {
		Date now = new Date();
		TransResult result = new TransResult();

		MdModel condition = new MdModel();
		condition.setModel(mdModel.getModel());
		condition.setActiveFlag("1");
		List<MdModel> list = mdModelService.findList(condition);
		if (list != null && !list.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该原材料型号代码已经存在！");
			return result;
		}

		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/add";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		mdModelMes.setMaterialName(mdModel.getModelDescZh());
		mdModelMes.setMaterialType(mdModel.getModelType());
		mdModelMes.setOutsideDiameter(mdModel.getOutsideDiameter() == null ? ""
				: mdModel.getOutsideDiameter().toString());
		mdModelMes.setThickness(mdModel.getThickness() == null ? "" : mdModel
				.getThickness().toString());
//		货主（0：本公司 1：外公司）
		mdModelMes.setOwner(mdModel.getUserDefined3());
		mdModelMes.setMeasureUnit(mdModel.getUnit());
		// 0：无效 1：有效
		mdModelMes.setMarkAble("1");

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.setGmtCreate(now);
			mdModel.setCreateBy(getLoginName());
			mdModel.setActiveFlag("1");
			Resp resp = mdModelService.create(mdModel);

			return fromResp(resp);
		}
	}

	/**
	 * 修改记录
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(MdModel mdModel) {
		TransResult result = new TransResult();
		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/update/wms";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		mdModelMes.setMaterialName(mdModel.getModelDescZh());
		mdModelMes.setMaterialType(mdModel.getModelType());
		mdModelMes.setOutsideDiameter(mdModel.getOutsideDiameter() == null ? ""
				: mdModel.getOutsideDiameter().toString());
		mdModelMes.setThickness(mdModel.getThickness() == null ? "" : mdModel
				.getThickness().toString());
//		货主（0：本公司 1：外公司）
		mdModelMes.setOwner(mdModel.getUserDefined3());
		mdModelMes.setMeasureUnit(mdModel.getUnit());

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.preUpdate();
			mdModel.setLastModifiedBy(getLoginName());
			Resp resp = mdModelService.update(mdModel);
			return fromResp(resp);
		}
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(MdModel mdModel) {
		TransResult result = new TransResult();
		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/update/wms";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		// 0：无效 1：有效
		mdModelMes.setMarkAble("0");

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.setActiveFlag("0");
			mdModel.setGmtModified(new Date());
			mdModel.setLastModifiedBy(getLoginName());
			Resp resp = mdModelService.delete(mdModel);
			return fromResp(resp);
		}
	}

	/**
	 * @Title list
	 * @Description 查询所有型号列表
	 * @author luoteng
	 * @date 2018年7月13日:上午10:11:26
	 * @param page
	 * @param rows
	 * @param mdModel
	 * @return TransResult
	 */
	@PostMapping("list")
	public TransResult list(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			MdModel mdModel) {
		mdModel.setActiveFlag("1");
		Pager<MdModel> result = mdModelService.findListByCondition(page, rows,
				mdModel);
		return success(result);
	}

	/**
	 * @Title whiteblackList
	 * @Description 查询黑白料型号列表
	 * @author luoteng
	 * @date 2018年7月13日:上午10:11:02
	 * @param page
	 * @param rows
	 * @param mdModel
	 * @return TransResult
	 */
	@PostMapping("whiteblack/list")
	public TransResult whiteblackList(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			MdModel mdModel) {
		mdModel.setActiveFlag("1");
		List<String> modelTypeList = new ArrayList<String>();
		modelTypeList.add(Constant.MODEL_B);
		modelTypeList.add(Constant.MODEL_H);
		mdModel.setModelTypeList(modelTypeList);
		Pager<MdModel> result = mdModelService.findListByCondition(page, rows,
				mdModel);
		return success(result);
	}

	/**
	 * @Title notWhiteblackList
	 * @Description 查询非黑白料型号列表
	 * @author luoteng
	 * @date 2018年7月13日:上午10:11:11
	 * @param page
	 * @param rows
	 * @param mdModel
	 * @return TransResult
	 */
	@PostMapping("not/whiteblack/list")
	public TransResult notWhiteblackList(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			MdModel mdModel) {
		mdModel.setActiveFlag("1");
		List<String> notModelTypeList = new ArrayList<String>();
		notModelTypeList.add(Constant.MODEL_B);
		notModelTypeList.add(Constant.MODEL_H);
		mdModel.setNotModelTypeList(notModelTypeList);
		Pager<MdModel> result = mdModelService.findListByCondition(page, rows,
				mdModel);
		return success(result);
	}

	/** 
	* @Title: chargeList 
	* @Description: 查询记账型号列表（热熔套，堵头） 
	* @param @param page
	* @param @param rows
	* @param @param mdModel
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("charge/list")
	public TransResult chargeList(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			MdProModel mdProModel) {
		mdProModel.setActiveFlag("1");
		List<String> modelTypeList = new ArrayList<String>();
		modelTypeList.add(Constant.MODEL_RRT);
		modelTypeList.add(Constant.MODEL_DUT);
		mdProModel.setModelTypeList(modelTypeList);
		Pager<MdProModel> result = mdProModelService.findListByCondition(page, rows,
				mdProModel);
		return success(result);
	}
	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		MdModel mdModel = mdModelService.findById(id);
		return success(mdModel);
	}
}
